import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user: {
      token: localStorage.getItem('token') || null // 从本地存储读取 token
    }
  },
  mutations: {
    SET_TOKEN(state, token) {
      state.user.token = token
      localStorage.setItem('token', token)
    },
    REMOVE_TOKEN(state) {
      state.user.token = null
      localStorage.removeItem('token')
    }
  },
  actions: {
    login({ commit }, token) {
      commit('SET_TOKEN', token)
    },
    logout({ commit }) {
      commit('REMOVE_TOKEN')
    }
  },
  modules: {}
})